{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <select name="channel_id" id="channel_id" lay-filter="channel_id" lay-search="">
            <option value="">选择渠道（全部）</option>
            {volist name="$channel_data" id="vo"}
            <option value="{$vo.cps_id}">{$vo.cps_id}：{$vo.cps_name}</option>
            {/volist}
        </select>
    </div>
    <div class="layui-inline">
        <select name="status" id="status" lay-filter="status">
            <option value="">对账状态（全部）</option>
            <option value="1">确认中</option>
            <option value="2">已确认</option>
        </select>
    </div>
    <div class="layui-inline" style="width:200px;">
        <div class="my-btn my-btn-total">确认中分成总额：<span id="money1">0</span></div>
    </div>
    <div class="layui-inline" style="width:200px;">
        <div class="my-btn my-btn-total">已确认分成总额：<span id="money2">0</span></div>
    </div>
    <div class="layui-inline" style="width: 420px;">
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="add">生成结算单</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="finish">已转账</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="setExpire">过期时间设置</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="handExpire">手动过期</button>
        <button class="layui-btn toolBtn my-btn my-btn-sm" type="button" id="export">导出</button>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="statusTpl">
    {{#  if(d.status === '已过期'){ }}
    <span class="layui-bg-cyan status4" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '确认中') { }}
    <span class="layui-bg-orange status1" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已确认') { }}
    <span class="layui-bg-blue status2" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } else if(d.status === '已转账') { }}
    <span class="layui-bg-green status3" val="{{ d.id }}" style="padding: 4px 6px;">{{ d.status }}</span>
    {{#  } }}
</script>

<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="view">查看</a>
    {{# if(d.status == '已转账'){ }}
    <button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="" lay-event="del">删除</button>
    {{#  }else{ }}
    <button class="layui-btn layui-btn-green layui-btn-sm status6" val="{{ d.id }}" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-danger layui-btn-sm status7" val="{{ d.id }}" lay-event="del">删除</button>
    {{#  } }}
</script>
{/block}

{block name="css"}
<style>
    table {border-collapse: separate;}
    .layui-table-cell {height: 62px; line-height:62px;}
    thead .layui-table-cell {line-height: 28px; height: 28px;}
</style>
{/block}

{block name="js"}
<script>
    layui.use(['table', 'form'], function(){
        var table = layui.table
            ,$ = layui.$
            ,load = null
            ,form = layui.form
            ,active = {
                reload: function () {
                    var channel_id = $('#channel_id').val()
                        ,status= $('#status').val();

                    table.reload('tableId', {
                        page: {curr: 1}
                        , where: {channel_id:channel_id,status:status}
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("AuditCpsMng/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function (res, curr, count) {
                res.params!==undefined && $('#money1').html(res.params.money1);
                res.params!==undefined && $('#money2').html(res.params.money2);
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {type:'checkbox', fixed: 'left'}
                ,{field:'cps_type', title: '类型', width: 70}
                ,{field:'daytime', title: '对账日期', width: 170}
                ,{field:'cps_id', title: '渠道ID', width: 70}
                ,{field:'cps_name', title: '渠道名称', width: 140}
                ,{field:'fact_money', title: '充值金额', width: 100, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'pay_rate', title: '分成', width: 70}
                ,{field:'channel_rate', title: '通道率', width: 70}
                ,{field:'tax_rate', title: '税率', width: 70}
                ,{field:'refer_money', title: '参考金额', width: 100}
                ,{field:'end_money', title: '分成金额', width: 100, style:'background-color: #6495ed; color: #fff;'}
                ,{field:'create_time', title: '发送时间', width: 150}
                ,{field:'sure_time', title: '渠道确认时间', width: 150}
                ,{field:'status', title: '状态', width: 100, templet: '#statusTpl', fixed: 'right'}
                ,{title: '操作', align:'center', width: 250, toolbar:'#tableBar', fixed: 'right'}
            ]]
        });

        //监听搜索下拉菜单
        form.on('select(channel_id)', function(data){
            load = layer.load();
            active['reload'].call(this);
        });

        //监听搜索下拉菜单
        form.on('select(status)', function(data){
            load = layer.load();
            active['reload'].call(this);
        });

        //监听搜索栏
        $('#searchBox .toolBtn').on('click', function(){
            if($(this).attr('id')=='setExpire'){
                layer.open({
                    type: 2
                    ,title: '过期时间设置'
                    ,area: ['100%', '100%']
                    ,content: '{:url("AuditCpsMng/setExpire")}'
                });
            }else if($(this).attr('id')=='finish'){
                var checkStatus = table.checkStatus('tableId')
                    ,data = checkStatus.data
                    ,str = '';

                if(data.length==0){
                    layer.msg('请选择要修改的条目');
                    return false;
                }

                for(key in data){
                    str += data[key].id+',';
                }
                var load = layer.load();
                $.post('{:url("AuditCpsMng/finish")}',{id: str.substring(0, str.length-1)}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        for (key in data){
                            $('.status2[val="'+data[key].id+'"]').text('已转账').addClass('layui-bg-green status3').removeClass('layui-bg-blue status2');
                        }
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.close(load);
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if($(this).attr('id')=='export'){
                var jsonStr = ''
                    ,jsonObj = {
                        "channel_id": $('#channel_id').val()
                        ,"status": $('#status').val()
                        ,"export": 1
                    };
                layer.msg('导出中....', {icon: 1, time: 2000});
                for(k in jsonObj){
                    jsonStr += k + '=' + jsonObj[k] + '&';
                }
                window.location.href = '{:url("AuditCpsMng/index")}?'+jsonStr.substring(0, jsonStr.length-1);
            }else if($(this).attr('id')=='handExpire'){
                layer.confirm('确定要将未确认的订单修改为已过期吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("AuditCpsMng/handExpire")}',{}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            $('.status1').text('已过期').addClass('layui-bg-cyan status4').removeClass('layui-bg-orange status1');
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }else if($(this).attr('id')=='add'){
                layer.open({
                    type: 2
                    ,title: '发送账单'
                    ,area: ['100%', '100%']
                    ,content: '{:url("AuditCpsMng/add")}'
                });
            }
        })

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['100%', '100%']
                    ,content: '{:url("AuditCpsMng/edit")}?id='+data.id
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("AuditCpsMng/del")}',{id: data.id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }else if(obj.event === 'view'){
                layer.open({
                    type: 2
                    ,title: '查看'
                    ,offset: '60px'
                    ,resize: true
                    ,area: ['1200px', '700px']
                    ,content: '{:url("AuditCpsMng/view")}?id='+data.id
                });
            }
        });
    });
</script>
{/block}